/*
 * Copyright 2011 OpenWAF.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.openwaf.client.ui.panels;

import com.openwaf.client.dom.Element;
import com.openwaf.client.dom.Node;
import com.openwaf.client.dom.TableCellElement;
import com.openwaf.client.dom.TableElement;
import com.openwaf.client.dom.TableRowElement;
import com.openwaf.client.dom.TableSectionElement;
import com.openwaf.client.ui.UIControl;
import com.openwaf.client.ui.annotation.ViewMethod;
import com.openwaf.client.ui.annotation.ViewProperty;
import java.util.ArrayList;
import java.util.Iterator;

public class VerticalPanel extends UIControl implements Iterable{

    private static String CSS_ELEMENT = "waf-panel-vertical-element";
    private static String CSS_ELEMENT_ALTERNATE = "waf-panel-vertical-element-alternate";
    private TableSectionElement tblBody;    
    private ArrayList elements;
    private String css_element;
    private String css_element_alternate;
    private boolean alternate_css_enabled=true;
    public VerticalPanel(){
        css_element=CSS_ELEMENT;
        css_element_alternate=CSS_ELEMENT_ALTERNATE;
        alternate_css_enabled=true;
        elements=new ArrayList();
    }
    @ViewProperty
    public void setElementCss(String className){
       css_element=className;
       updateCssClass();
    }
    @ViewProperty
    public void setEnableAlternateCss(String value){
        if(Boolean.parseBoolean(value)){
            enableAlternateCss();
        }else{
            disableAlternateCss();
        }        
    }
    @ViewProperty
    public void setElementAlternateCss(String className){
        alternate_css_enabled=true;
        css_element_alternate=className;
        updateCssClass();
    }
    @ViewProperty
    public void enableAlternateCss(){
        this.alternate_css_enabled=true;
        updateCssClass();
    }
    @ViewProperty
    public void disableAlternateCss(){
        this.alternate_css_enabled=false;
        updateCssClass();
    }
    public boolean isAlternateCssEnabled(){
        return this.alternate_css_enabled;
    }
    @ViewProperty
    public void setBorderWidth(int width){
        ((TableElement)this.getElement()).setBorder(width);
    }
    public int getBorderWidth(){
        return Integer.parseInt(((TableElement)this.getElement()).getBorder());
    }
    private void updateCssClass(){
        if(!alternate_css_enabled)
            for(TableRowElement row:tblBody.getRows()){
                row.getCells()[0].setCssClassName(css_element);
            }
        else{
            boolean alternate=false;
            for(TableRowElement row:tblBody.getRows()){
                if(alternate){
                    row.getCells()[0].setCssClassName(css_element_alternate);
                }else{                
                    row.getCells()[0].setCssClassName(css_element);
                }
                alternate=!alternate;
            }

        }
    }
    @ViewMethod
    public void addElement(Element elm){
        TableRowElement row=tblBody.insertRow(elements.size());
        TableCellElement cell=row.insertCell(0);
        elements.add(elm);
        cell.appendChild(elm);
        if(alternate_css_enabled==true){
            if(elements.size()%2==0){
                cell.setCssClassName(css_element);
            }else{
                cell.setCssClassName(css_element_alternate);
            }
        }else{
            cell.setCssClassName(css_element);
        }
    }
    @ViewMethod
    public void addElement(UIControl c){
        TableRowElement row=tblBody.insertRow(elements.size());
        TableCellElement cell=row.insertCell(0);
        elements.add(c);
        cell.appendChild(c.getElement());
        if(alternate_css_enabled==true){
            if(elements.size()%2==0){
                cell.setCssClassName(css_element);
            }else{
                cell.setCssClassName(css_element_alternate);
            }
        }else{
            cell.setCssClassName(css_element);
        }
    }
    public void addElement(int index,Element elm){
        if(index<0 || index>=elements.size()){
            throw new IndexOutOfBoundsException("Index out of bounds VerticalPanel::addElementAt");
        }        
        TableRowElement row=tblBody.insertRow(index);
        TableCellElement cell=row.insertCell(0);
        elements.add(elm);
        cell.appendChild(elm);
    }
    public void addElement(int index,UIControl c){
    if(index<0 || index>=elements.size()){
            throw new IndexOutOfBoundsException("Index out of bounds VerticalPanel::addElementAt");
        }
        
        TableRowElement row=tblBody.insertRow(index);
        TableCellElement cell=row.insertCell(0);
        elements.add(c);
        cell.appendChild(c.getElement());
    }
    public int getElementCount(){
        return elements.size();
    }
    public int indexOf(Object o){
        return elements.indexOf(o);
    }
    public int lasIndexOf(Object o){
        return elements.lastIndexOf(o);
    }
    public Object getElement(int i){
        if(i>=0 && i<elements.size()){
            return elements.get(i);
        }
        throw new IndexOutOfBoundsException("Index out of bounds in VerticalPanel::getElementAt");
    }
    public boolean contains(Object o){
        return elements.contains(o);
    }  
    public boolean removeElement(Object o){
        int i=elements.indexOf(o);
        if(i>=0){
            elements.remove(o);
            tblBody.deleteRow(i);
            return true;
        }
        return false;
    }
    public boolean removeElement(int index){
        if(index<0 || index>=elements.size()){
            throw new IndexOutOfBoundsException("Index out of bounds at VerticalPanel::removeElementAt");
        }
        Object o=elements.get(index);
        tblBody.deleteRow(index);
        elements.remove(o);
        return true;
    }
    public boolean isEmpty(){
        return elements.isEmpty();
    }
    public void clear(){
       
        int i,len=elements.size();
        for(i=len-1;i>=0;i--){
            tblBody.deleteRow(i);
        }
         elements.clear();
    }
    public Iterator iterator() {
        return elements.iterator();
    }
    
}
